function Site(data) {
    this.id = data.id;
    this.title = data.title? data.title : '';
}

function Url(data) {
    this.id = data.id;
    this.site_id = data.site_id;
    this._title = data.title;
}

Url.prototype = {
    get title () {
        return this._title? this._title : '#' + this.id;
    },
    set title (title) {
        this._title = title;
    }
};

function Msg(data) {
    this.id = data.id;
    this.site_id = data.site_id;
    this.title = data.title? data.title : '';
    this.content = data.content;
}

function DataProvider() {
    var self = this;

    this.api = function(uri) {
        return 'http://pa.stepanets.org' + uri;
    };

    self.onSitesUpdate = function(){};

    self.updateSites = function(data) {
        var sites = data.map(function(site) {
            return new Site(site);
        });
        self.onSitesUpdate(sites);
    };

    self.getSites = function(callback) {
        $.ajax({
            dataType: "json",
            url: self.api('/site?format=json&callback=?'),
            success: function(data) {
                if( data.error && data.error.code ) return;
                var sites = data.sites.map(function(site) {
                    return new Site(site);
                });
                callback(sites);
            }
        });
    };

    self.getUrls = function(callback) {
        $.ajax({
            dataType: "json",
            url: self.api('/url?format=json&callback=?'),
            success: function(data) {
                if( data.error && data.error.code ) return;
                var urls = data.urls.map(function(url) {
                    return new Url(url);
                });
                callback(urls);
            }
        });
    };

    self.getMessages = function(params, callback) {
        $.ajax({
            dataType: "json",
            data: params,
            url: self.api('/msg?format=json&callback=?'),
            success: function(data) {
                if( data.error && data.error.code ) return;
                var msgs = data.messages.map(function(msg) {
                    return new Msg(msg);
                });
                callback(msgs);
            }
        });
    };

}

function AggViewModel() {
    var self = this;
    self.DataProvider = null;

    self.sites = ko.observableArray([]);
    self.current_site = ko.observable();

    self.urls = ko.observableArray([]);
    self.current_urls = ko.observableArray([]);

    self.messages = ko.observableArray([]);
    self.current_message = ko.observable();

    self.goToSite = function(site) {
        self.current_site(site);
        var urls = self.urls().filter(function(url) {
            return url.site_id === site.id;
        });
        self.current_urls(urls);
        $.mobile.changePage('#urls');
    };

    self.goToUrl = function(url) {
        self.DataProvider.getMessages({
            url: url.id
        }, function(msgs) {
            self.messages(msgs);
        });
    };

    self.start = function() {
        /*self.DataProvider.getSites(function(sites) {
            self.sites(sites);
        });*/

        self.DataProvider.onSitesUpdate = function(sites){
            self.sites(sites);
        };

        self.DataProvider.getSites(function(site) {
            self.sites(site);
        });

        self.DataProvider.getUrls(function(urls) {
            self.urls(urls);
        });
    };
}

$(document).ready(function(){
    var $vm = new AggViewModel();
    $dp = new DataProvider();
    $vm.DataProvider = $dp;
    ko.applyBindings($vm);

    $vm.start();
    /*$.ajax({
        dataType: "json",
        url: 'http://pa.stepanets.org/site?format=json&callback=?',
        success: function(data) {
            if( data.error && data.error.code ) return;
            $dp.updateSites(data.sites);

        }
    });*/

});
